package net.xinqushi.design.holder;

import java.net.InetAddress;

/**
 * @author xjie
 * @description  Socket源码中 神似Builder的一种用法
 */
public class SocketAddress {
    // Private implementation class pointed to by all public methods.
    private static class SocketAddressHolder {
        // The hostname of the Socket Address
        private String hostname;
        // The IP address of the Socket Address
        private InetAddress addr;
        // The port number of the Socket Address
        private int port;

        private SocketAddressHolder(String hostname, InetAddress addr, int port) {
            this.hostname = hostname;
            this.addr = addr;
            this.port = port;
        }

        private int getPort() {
            return port;
        }

        private InetAddress getAddress() {
            return addr;
        }

        private String getHostName() {
            if (hostname != null)
                return hostname;
            if (addr != null)
                return addr.getHostName();
            return null;
        }
    }

    // private constructor for creating unresolved instances
    public SocketAddress(int port, String hostname) {
        holder = new SocketAddressHolder(hostname, null, port);
    }

    private final transient SocketAddressHolder holder;


}
